package io.jboot.fly.service.impl;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import io.jboot.aop.annotation.Bean;
import io.jboot.db.model.Columns;
import io.jboot.fly.model.Post;
import io.jboot.fly.model.UserAttachment;
import io.jboot.fly.service.UserAttachmentService;
import io.jboot.service.JbootServiceBase;

import javax.inject.Singleton;
import java.util.List;

@Bean
@Singleton
public class UserAttachmentServiceImpl extends JbootServiceBase<UserAttachment> implements UserAttachmentService {
    @Override
    public Page<UserAttachment> paginateByParams(int pageNumber, int pageSize,String userId) {
        if(!"".equals(userId)) {
            Columns columns = Columns.create("relUserId", userId);
            return DAO.paginateByColumns(pageNumber,pageSize, columns.getList(), "createdTime desc");
        }else{
            return DAO.paginate(pageNumber,pageSize,"createdTime desc");
        }

    }

    @Override
    public UserAttachment findByRelPostId(String postId) {

        return DAO.findFirstByColumn("rel_post_id",postId);

    }

    @Override
    public Page<UserAttachment> paginateByCategory(int pageNumber, int pageSize, String attachmentCategoryId) {
        if(!"".equals(attachmentCategoryId)) {
            Columns columns = Columns.create("attachmentCategoryId", attachmentCategoryId);
            return DAO.paginateByColumns(pageNumber,pageSize, columns.getList(), "createdTime desc");
        }else{
            return DAO.paginate(pageNumber,pageSize,"createdTime desc");
        }
    }

    @Override
    public int myFileDownLoadedCount(String userId) {

        return Db.queryInt("select count(*) from user_action where action='upload' and post_id in \n" +
                "(select p.id from post p where p.user_id = ? and p.userAttachment_id is not null)",userId);
    }
}